clear
capture restore
set scheme cleanplots, perm

* SET DIRECTORIES
global home "..."

global data "$home/Data/"
global output "$home/Output/"

cd $home

*SET CONTROL VARIABLES
local firm_controls "log_salesg_real_1lag_std size_assets_1lag_std liquidity_1lag_std pcmq_1lag_std recpay2yq_1lag_std d2aq_1lag_std ln_market_cap_q_1lag_std tobins_q_1lag_std lev_d2c_1lag_std i.sector i.fqtr"

local sum_stats "log_salesg_real_1lag size_assets_1lag ln_market_cap_q_1lag liquidity_1lag pcmq_1lag recpay2yq_1lag d2aq_1lag tobins_q_1lag lev_d2c_1lag dd_1lag age"

local agg_controls "lngdp inflation unemp nasdaq fedfunds"

*************
***Options***
*************
*Set ivol measure to use as baseline
local matur m1545
*Set mp shock to use as baseline
local mps ff4_d

* Set the LP details ========================================================================
*# periods to compute the responses foreach
local hor = 0
*# lags of "endogenous" variables 
local lagy = 4 
*# lags of "shock"
local lage = 0 

********************************************************************************
********************************************************************************
********************************************************************************
*Read-in ivol data (contains all trading days for Compustat firms)
use "${data}Compustat_Quarterly_Final", clear

local n = 20

*Set panel data
xtset firmid ts_qtr

*Flag sample for those in at least n quarters
bysort gvkey pre_crisis: egen obs_num_temp = count(gvkey) if pre_crisis
bysort gvkey: egen obs_num = max(obs_num_temp)

*Flag outliers (bottom/top 0.5%) for investment
_pctile investment_intensive if pre_crisis, p(.5 99.5)
gen 	investment_outlier = (investment_intensive<r(r1)|investment_intensive>r(r2))

gen sample_period=(pre_crisis==1)

gen in_sample = (obs_num>=`n' & !missing(obs_num) & investment_outlier==0 & sample_period)

********************************************************************************
*Set panel data
xtset firmid ts_qtr

*Standardize mps to unit standard deviation & positive is contracionary
gen `mps'=-`mps'_shock_qtr

xtset firmid ts_qtr

*Use the inflation-adjusted versions of dependent variables, if applicable
foreach i in K_int_Know K_int_Org {
	
	drop `i'
	rename `i'_real `i'
 
}


foreach var in capital_stock K_int_Know_qtr K_int_Org_qtr {
	forvalues h =   0/`hor' {
		 gen `var'`h' = ln(f`h'.`var') - ln(l.`var')
}
}

********************************************************************************
********************************************************************************
keep if in_sample

drop if year==1995

egen date_tag=tag(ts_qtr)

***Flag firms as high uncertainty
bysort ts_qtr: egen ivol_70th = pctile(ivol_`matur'), p(70)
bysort ts_qtr: egen ivol_30th = pctile(ivol_`matur'), p(30) 

*SUMMARY STATISTICS OF MP SHOCKS, IVOL, DD, etc.
order ivol_`matur' dd capital_stock0 K_int_Know_qtr0 K_int_Org_qtr0

preserve 
keep if !missing(ivol_`matur')
qui: outreg2 using "${output}tableA1_panelA.xls", excel dec(2) replace sum(log) eqkeep(N mean sd) keep(ivol_`matur' dd capital_stock0 K_int_Know_qtr0 K_int_Org_qtr0)
restore

preserve 
keep if date_tag
qui: outreg2 using "${output}tableA1_panelB.xls", excel dec(2) replace sum(log) eqkeep(N mean sd) keep(`mps')
restore

preserve
keep if !missing(ivol_`matur') & ivol_`matur'>=ivol_70th
qui: outreg2 using "${output}tableA1_panelD_highUncertainty.xls", excel dec(2) replace sum(log) eqkeep(N mean sd) keep(ivol_`matur' `sum_stats' capital_stock0 K_int_Know_qtr0 K_int_Org_qtr0)
restore

preserve
keep if !missing(ivol_`matur') & ivol_`matur'<=ivol_30th
qui: outreg2 using "${output}tableA1_panelD_lowUncertainty.xls", excel dec(2) replace sum(log) eqkeep(N mean sd) keep(ivol_`matur' `sum_stats' capital_stock0 K_int_Know_qtr0 K_int_Org_qtr0)
restore
